#pragma once

#include <iostream>
#include <string>
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"

using namespace TcpServerModule;

const std::string sep = "\r\n";
const std::string BlankLine = sep;

class HttpServer
{
public:
    HttpServer(int port)
        : _tsvr(std::make_unique<TcpServer>(port))
    {
    }

    void Start()
    {
        _tsvr->InitServer([this](SockPtr sockfd, InetAddr client)
                          { return this->HandlerHttpRequest(sockfd, client); });

        _tsvr->Loop();
    }

    bool HandlerHttpRequest(SockPtr sockfd, InetAddr client)
    {
        LOG(LogLevel::DEBUG) << "HttpServer: get a new client: " << sockfd->Fd()
                             << " addr info: " << client.Addr();
        std::string request;
        sockfd->Recv(&request);
        std::cout << "HttpServer: recv a request: " << request << std::endl;

        // 读取请求，对请求进行分析处理--> 文本处理
        // 1.读取请求的完整性
        // 2.完整http反序列化，http response序列化...
        // 1.返回一个固定的内容
        // std::string status_line = "HTTP/1.1 200 OK" + sep + BlankLine;

        // // 直接返回一个html网页
        // std::string body = "<!DOCTYPE html>\
        // <html>\
        // <head>\
        // <title>Hello World</title>\
        // </head>\
        // <body>\
        // <h1>Hello World</h1>\
        // </body></html>";

        // std::string httpresponse=status_line+body;
        // sockfd->Send(httpresponse);
        return true;
    }
    ~HttpServer()
    {
    }

private:
    std::unique_ptr<TcpServer> _tsvr;
};